package com.firsteapps.mobile.android.puzzle.game;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.firsteapps.mobile.android.puzzle.PostLoginBase;
import com.firsteapps.mobile.android.puzzle.R;
import com.firsteapps.mobile.android.puzzle.SelectPuzzle;
import com.firsteapps.mobile.android.puzzle.util.Consts;
import com.firsteapps.mobile.android.puzzle.util.Utilities;

public class ViewComplPuzzles extends PostLoginBase {

	private TextView puzzleSizeViewer, movesCountViewer, attemptsCountViewer, timeLimitViewer, tilesCountViewer, picturePath;
	private int currentIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.continue_layout);

		findViewById(R.id.arrow_layout).setVisibility(View.VISIBLE);

		puzzleSizeViewer = (TextView) findViewById(R.id.continue_puzzle_size);
		movesCountViewer = (TextView) findViewById(R.id.continue_puzzle_moves_count);
		attemptsCountViewer = (TextView) findViewById(R.id.continue_puzzle_attempts_count);
		timeLimitViewer = (TextView) findViewById(R.id.continue_puzzle_time_count);
		tilesCountViewer = (TextView) findViewById(R.id.continue_puzzle_tiles_count);
		picturePath = (TextView) findViewById(R.id.continue_picture_path);

		final String[] completedPuzzles = Helper.getPuzzleData(this, getWorkingDirectory() + Consts.FOLDER_NAME_COMPLETED_PUZZLES);
		if (completedPuzzles != null && completedPuzzles.length > 0) {
			loadSelectedPuzzle(this, completedPuzzles[currentIndex]);

			// ImageView continueOld = (ImageView) findViewById(R.id.continue_continue);
			// continueOld.setOnClickListener(new OnClickListener() {
			// @Override
			// public void onClick(View v) {
			// Intent myIntent = new Intent(ViewComplPuzzles.this, GameScreen.class);
			// myIntent.putExtra(Consts.PUZZLE_KEY, completedPuzzles[currentIndex]);
			// startActivity(myIntent);
			// finish();
			// overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
			// }
			// });

			findViewById(R.id.left_arrow).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (completedPuzzles.length > 1) {
						currentIndex = (currentIndex + completedPuzzles.length - 1) % completedPuzzles.length;
						loadSelectedPuzzle(ViewComplPuzzles.this, completedPuzzles[currentIndex]);
					}
				}
			});

			findViewById(R.id.right_arrow).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (completedPuzzles.length > 1) {
						currentIndex = (currentIndex + 1) % completedPuzzles.length;
						loadSelectedPuzzle(ViewComplPuzzles.this, completedPuzzles[currentIndex]);
					}
				}
			});
		}
		
		findViewById(R.id.continue_start_new).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Utilities.startActivityWithAnimation(ViewComplPuzzles.this, SelectPuzzle.class);
			}
		});
	}

	private void loadSelectedPuzzle(Context context, String filePath) {
		PuzzleInfo info = Helper.loadCreatedPuzzle(context, filePath);

		puzzleSizeViewer.setText(info.getSize() + ":" + info.getSize());
		movesCountViewer.setText(info.getMoveCount() + (info.getMaxMoves() > 0 ? "/" + info.getMaxMoves() : ""));
		attemptsCountViewer.setText(info.getAttempts() + (info.getMaxAttempts() > 0 ? "/" + info.getMaxAttempts() : ""));
		timeLimitViewer.setText(Helper.getElapsedTime(info.getElapsed())
				+ (info.getMaxTime() > 0 ? "/" + Helper.getElapsedTime(info.getMaxTime() * 1000) : ""));
		tilesCountViewer.setText(info.getSize() * info.getSize() + "");
		String pictureFileName = info.getFile();
		pictureFileName = pictureFileName.substring(pictureFileName.lastIndexOf("/") + 1);
		pictureFileName = pictureFileName.substring(0, pictureFileName.indexOf(".")) + ".jpg";
		picturePath.setText(pictureFileName);
	}
}